package com.training.permission.dao.impl.chengshu;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.training.perimission.dao.chengshu.IPermission;
import com.training.pojo.chengshu.FunUser;
import com.training.pojo.chengshu.IsRolePermission;
import com.training.pojo.chengshu.UFunction;
import com.training.pojo.chengshu.URole;
import com.training.pojo.chengshu.UUser;
import com.training.pojo.chengshu.UserRole;

public class PermissionImpl extends HibernateDaoSupport implements IPermission {

	// 查询员工
	@SuppressWarnings("unchecked")
	public List<UUser> listUser(Integer start, Integer max, String str) {
		Session session = this.getSession();
		String hql = null;
		Query query = null;
		try {
			if (str == null) {
				hql = "from UUser as uu where isdelete = 1";
				System.out.println(11111);
				query = session.createQuery(hql);
			} else {
				if (str.equals("男")) {
					hql = "from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				} else if (str.equals("女")) {
					hql = "from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				} else {
					hql = "from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				}
				query = (Query) session.createQuery(hql);
				query.setString(0, "%" + str + "%");
				query.setString(1, "%" + str + "%");
			}
			query.setFirstResult(start - 1);
			query.setMaxResults(max);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<UUser> list = query.list();
		System.out.println(list.size() + "长度");
		for (UUser uu : list) {
			System.out.println("遍历");
			System.out.println(uu.getUname());
		}
		return list;
	}

	// 查询员工数量
	@SuppressWarnings("unchecked")
	public Integer countUser(String str) {
		Session session = this.getSession();
		String hql = null;
		Query query = null;
		Integer count = null;
		try {
			if (str == null) {
				hql = "select count(*) from UUser as uu where isdelete = 1";
				query = (Query) session.createQuery(hql);
				String s = query.list().get(0).toString();
				count = Integer.parseInt(s);
			} else {
				if (str.equals("男")) {
					hql = "select count(*) from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				} else if (str.equals("女")) {
					hql = "select count(*) from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				} else {
					hql = "select count(*) from UUser as uu where uu.uname like ? or uu.UDept.deptid in (select ud.deptid from UDept as ud where ud.deptname like ?) and uu.isdelete = 1 ";
				}
				query = (Query) session.createQuery(hql);
				query.setString(0, "%" + str + "%");
				query.setString(1, "%" + str + "%");
				System.out.println();
				System.out.println(str);
				System.out.println();
				List<Long> list = query.list();
				if (list.size() > 0) {
					String s = (String) query.list().get(0).toString();
					System.out.println(s);
					count = Integer.parseInt(s);
					System.out.println("数量：" + count);
				} else {
					count = 0;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	private void list() {
		// TODO Auto-generated method stub

	}

	// 查询角色
	@SuppressWarnings("unchecked")
	public List<URole> listRole(Integer start, Integer max, String str) {
		Session session = this.getSession();
		session.clear();
		String hql = null;
		Query query = null;
		try {
			if (str == null) {
				hql = "from URole as ur where 1 = 1";
				query = session.createQuery(hql);
			} else {
				hql = "from URole as ur where ur.rolename like ? and ur.isdelete = 1";
				query = (Query) session.createQuery(hql);
				query.setString(0, "%" + str + "%");
			}
			query.setFirstResult(start - 1);
			query.setMaxResults(max);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<URole> list = query.list();
		return list;
	}

	// 查询角色数量
	@SuppressWarnings("unchecked")
	public Integer countRole(String str) {
		HibernateTemplate htp = this.getHibernateTemplate();
		htp.clear();
		String hql = null;
		if (str == null) {
			hql = "select count(*) from URole as ur ";
			List<Long> list = htp.find(hql);
			Integer count = Integer.parseInt(list.get(0).toString());
			return count;

		} else {
			hql = "select count(*) from URole as ur where ur.rolename like ? and ur.isdelete = 1";
			List<Long> list = htp.find(hql, "%" + str + "%");

			Integer count = Integer.parseInt(list.get(0).toString());
			return count;
		}
	}

	// 新增角色
	@SuppressWarnings("unchecked")
	public boolean newRole(URole role) {
		boolean b = true;
		List<URole> ur = new ArrayList<URole>();
		URole u = null;
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		hql = "from URole as ur where ur.rolename = ?";
		try {
			ur = htp.find(hql, role.getRolename());
			if (ur.size() > 0) {
				u = ur.get(0);
			}
			if (u != null && u.getRoleid() > 0) {
				role = u;
				role.setIsdelete(1);
				htp.saveOrUpdate(role);
			} else {
				role.setIsdelete(1);
				htp.save(role);
			}
		} catch (Exception e) {
			e.printStackTrace();
			b = false;
		}
		return b;

	}

	// 删除角色
	@SuppressWarnings("unchecked")
	public boolean delRole(List<Integer> id) {
		boolean b = true;
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		List<URole> u = new ArrayList<URole>();
		URole ur = null;
		hql = "from URole as ur where ur.roleid = ?";
		try {
			for (Integer i : id) {
				u = htp.find(hql, i);
				System.out.println("更新");
				ur = u.get(0);
				ur.setIsdelete(0);
				htp.saveOrUpdate(ur);
			}
		} catch (Exception e) {
			e.printStackTrace();
			b = false;
		}
		return b;
	}

	// 修改员工角色
	@SuppressWarnings("unchecked")
	public boolean updateUserRole(UserRole role) {
		boolean b = true;
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		System.out.println(role.getUUser().getUid()+":::条件"+role.getURole().getRoleid());
		hql = "from UserRole where UUser.uid = ?";
		List<UserRole> list = new ArrayList<UserRole>();
		try {
			list = htp.find(hql, role.getUUser().getUid());
			System.out.println("长度："+list.size());
			if(list.size()>0){
				htp.delete(list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			htp.saveOrUpdate(role);
		} catch (Exception e) {
			e.printStackTrace();
			b = false;
		}
		return b;
	}

	// 遍历角色
	@SuppressWarnings("unchecked")
	public List<URole> listRole(Integer id) {
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		List<Integer> list1 = new ArrayList<Integer>();
		hql = "select URole.roleid from UserRole where UUser.uid = ? ";
		try {
			list1 = htp.find(hql, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		id = -1;
		if (list1.size() > 0) {
			id = list1.get(0);
		}

		List<URole> list = new ArrayList<URole>();

		try {
			if (id > 0) {
				hql = "from URole as ur where 1 = 1 and ur.roleid != ?";
				list = htp.find(hql, id);
			} else {
				hql = "from URole as ur where 1 = 1";
				list = htp.find(hql);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}

	// 搜索员工的角色号
	@SuppressWarnings("unchecked")
	public URole queryRoleId(Integer id) {
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		List<URole> list = new ArrayList<URole>();
		List<UserRole> li = new ArrayList<UserRole>();
		hql = "from UserRole as url where url.UUser.uid = ?";
		try {
			li = htp.find(hql, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		hql = "from URole as ur where ur.roleid = ?";
		URole ur = null;
		if (li.size() > 0) {
			try {
				list = htp.find(hql, li.get(0).getURole().getRoleid());
				if (list.size() > 0) {
					ur = list.get(0);
					System.out.println(ur.getRolename() + "::======="
							+ ur.getRoleid());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ur;
	}

	//遍历角色权限
	@SuppressWarnings("unchecked")
	public List<UFunction> listuf(URole role) {
		HibernateTemplate htp = this.getHibernateTemplate();
		List<UFunction> list = new ArrayList<UFunction>();
		List<FunUser> li = new ArrayList<FunUser>();
		String hql = null;
		hql = "from FunUser as fu where fu.URole.roleid = ? and ischeck = 1";
		try {
			li = htp.find(hql,role.getRoleid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(li.size()>0){
			hql = "from UFunction as uf where uf.modeid = 2 and uf.fid = ?";
			try {
				for(FunUser fu : li){
					list.addAll(htp.find(hql,fu.getUFunction().getFid()));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}

	//遍历所有权限
	@SuppressWarnings("unchecked")
	public List<UFunction> listAllUF() {
		HibernateTemplate htp = this.getHibernateTemplate();
		List<UFunction> list = new ArrayList<UFunction>();
		String hql = null;
		hql = "from UFunction as uf where uf.rack = 1";
		try {
			list = htp.find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	//设置前台权限显示
	public List<IsRolePermission> listRolePer(List<UFunction> list,
			List<UFunction> allList) {
		List<IsRolePermission> resultList = new ArrayList<IsRolePermission>();
		IsRolePermission irp = new IsRolePermission();
		Integer i = 0;
		for(int m = 0;m<allList.size();m++){
			irp.setUfnction(allList.get(0));
			irp.setId(i);
			resultList.add(irp);
			allList.remove(0);
			
		}
		
		return resultList;
	}

	//修改权限
	@SuppressWarnings("unchecked")
	public boolean updatePermission(String[] id,String roleid) {
		boolean b = true;
		System.out.println("节点1");
		HibernateTemplate htp = this.getHibernateTemplate();
		String hql = null;
		FunUser funuser = new FunUser(); 
		List<FunUser> list = new ArrayList<FunUser>();
		for(int i=0;i<id.length;i++){
			hql = "from FunUser as fu where fu.UFunction.fid = ? and fu.URole.roleid = ?";
			try {
				System.out.println("节点2");
				
				list = htp.find(hql,Integer.parseInt(id[i]),Integer.parseInt(roleid));
				if(list.size()>0){
					funuser = list.get(0);
					funuser.setIscheck(1);
					System.out.println("节点3");
					htp.saveOrUpdate(funuser);
					System.out.println("节点4");
				}else{
					System.out.println(id[i]+"毛病"+roleid);
					
					System.out.println("节点5");
					htp.saveOrUpdate(funuser);
					System.out.println("节点6");
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("节点7");
				b = false;
			}
		}
		return b;
	}

}
